﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Resources;
using System.Globalization;



namespace QLCHQA
{
    public partial class Thay_Doi_MK : Form
    {
        string id;        
        DataSet ds;
        SqlDataAdapter da;
        DataTable dt;
        public Thay_Doi_MK()
        {
            InitializeComponent();
        }

        public void chuyendl(string id)
        {
            this.id = id;
         }

        private void Thay_Doi_MK_Load(object sender, EventArgs e)
        {
            this.tbmkc.Focus();
        }

        private void bttdmk_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
            string sql = "SELECT * FROM nhanvien WHERE id='" + id + "'";
            dt = new DataTable();
            ds = new DataSet();
            da = new SqlDataAdapter(sql, conn);
            da.Fill(ds,"crd");
            dt = ds.Tables["crd"];
            DataRow dr = dt.Rows[0];
            string mkc = dr[2]+"";
            string mkmtb = tbmkc.Text;

            if (mkc.Trim() != mkmtb.Trim())
            {
                MessageBox.Show("Mật khẩu cũ chưa đúng, vui lòng nhập lại");
                tbmkc.Clear();
                tbmkc.Focus();
                tbmkm.Clear();
                tbmkmr.Clear();
            }
            else
                if (tbmkmr.Text != tbmkmr.Text)
                {
                    MessageBox.Show("Nhập lại mật khẩu mới chưa đúng, vui lòng thử lại");
                    tbmkc.Clear();
                    tbmkc.Focus();



                    tbmkm.Clear();
                    tbmkmr.Clear();
                }
                else
                {
                    string mkm = tbmkm.Text;
                    string sqlup = "UPDATE nhanvien SET mk='"+mkm+"'where id='" + id + "'";
                    SqlCommand cmd = new SqlCommand(sqlup, conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    if (MessageBox.Show("Đổi mật khẩu thành công","", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        this.Hide();
                    }
                    Close();  
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

    }

}
